package com.liang.demo.controller;

import com.liang.demo.base.BaseApiController;
import com.liang.demo.base.RestResponse;
import com.liang.demo.base.SystemCode;
import com.liang.demo.po.Admin;
import com.liang.demo.po.Role;
import com.liang.demo.service.AdminService;
import com.liang.demo.service.MenuService;
import com.liang.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 陈琪亮
 * @version 1.0
 * @date 2021/3/18 16:39
 */
@RestController
@RequestMapping("/api")
public class AdminController extends BaseApiController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @RequestMapping(value = "/refreshToken", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse refreshToken(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        String refreshToken = adminService.refreshToken(token);
        System.out.println(token);
        if (refreshToken == null) {
            return RestResponse.fail(SystemCode.AccessTokenError.getCode(),SystemCode.AccessTokenError.getMessage());
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", refreshToken);
        tokenMap.put("tokenHead", tokenHead);
        return RestResponse.ok(tokenMap);
    }

    @RequestMapping(value = "/admin/login", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse login(@RequestBody Admin admin) {
        String token = adminService.login(admin.getUserName(), admin.getPassword());
        if (token == null) {
            return RestResponse.fail(SystemCode.AuthError.getCode(),SystemCode.AuthError.getMessage());
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return RestResponse.ok(tokenMap);
    }

    @GetMapping("/admin/info")
    public RestResponse getInfo() {
        Admin admin = getCurrentUser();
        if (admin == null) {
            return RestResponse.fail(SystemCode.UNAUTHORIZED.getCode(), SystemCode.UNAUTHORIZED.getMessage());
        }

        Map<String, Object> data = new HashMap<>();
        //获取用户名
        data.put("username", admin.getNickName());
        //获取菜单
        data.put("menus", menuService.getMenuList(admin.getId()));
        //获取角色
        List<Role> roleList = roleService.getRoleList(admin.getId());
        if (roleList != null && roleList.size() > 0) {
            List<String> roles = roleList.stream().map(Role::getName).collect(Collectors.toList());
            data.put("roles", roles);
        }
        //获取用户头像
        data.put("icon", admin.getIcon());
        return RestResponse.ok(data);
    }

}
